package cn.jwelll.bxcstock.ui.view

import android.content.Context
import android.os.Build
import android.text.Html
import android.util.AttributeSet
import android.view.View
import android.widget.ListView
import cn.jwelll.bxcstock.R
import cn.jwelll.bxcstock.bean.UploadDeleteChildEvent
import cn.jwelll.bxcstock.ui.adapter.CommonAdapter
import cn.jwelll.bxcstock.ui.dialog.DialogUtils
import cn.jwelll.bxcstock.viewmodel.itemmodel.ItemCarInPreviewModel
import cn.jwelll.bxcstock.viewmodel.itemmodel.ItemCarInUploadModel
import cn.jwelll.bxcstock.viewmodel.itemmodel.UploadDetailModel
import kotlinx.android.synthetic.main.item_carin_upload_detail.view.*
import org.greenrobot.eventbus.EventBus


class AutoListView : ListView {

    constructor(context: Context) : super(context)
    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)

    constructor(context: Context, attrs: AttributeSet?, defStyle: Int) : super(context, attrs, defStyle)

    init {
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        val expandSpec = View.MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE shr 2, View.MeasureSpec.AT_MOST)
        super.onMeasure(widthMeasureSpec, expandSpec)
    }

    var itemCarinThreee: ItemCarInUploadModel? = null
        set(value) {
            field = value
            value?.let {
                if (it!!.childList.isNotEmpty()) {
                    adapter =
                        CommonAdapter(context, R.layout.item_carin_upload_detail, it.childList) { itemView, t, i ->
                            itemView.tv_delete.setOnClickListener {
                                var charSequence: CharSequence = ""
                                var value = "你确定要删除捆包<font color=\"#222222\">${t?.materialCode}</font>吗？"
                                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                                    charSequence = Html.fromHtml(value, Html.FROM_HTML_MODE_COMPACT)
                                } else {
                                    charSequence = Html.fromHtml(value)
                                }
                                DialogUtils.Builder(context).setMessage(charSequence)
                                    .setNegativeButton("取消", R.color.color_999999, null)
                                    .setPositiveButton("确定", R.color.color_2d5aff) {
                                        field?.index?.let { it1 ->
                                            EventBus.getDefault().post(UploadDeleteChildEvent(it1, i, t?.weight!!))
                                        }
                                    }
                                    .creat().show()
                            }
                        }
                }
            }
        }
    var itemCarinThree: ArrayList<UploadDetailModel>? = null
        set(value) {
            field = value
            value?.let {
                if (it!!.isNotEmpty()) {
                    adapter = CommonAdapter(context, R.layout.item_carin_upload_detail, it) { itemView, t, i ->
                        itemView.tv_delete.setOnClickListener {
                            var charSequence: CharSequence = ""
                            var value = "你确定要删除捆包<font color=\"#222222\">${t?.materialCode}</font>吗？"
                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                                charSequence = Html.fromHtml(value, Html.FROM_HTML_MODE_COMPACT)
                            } else {
                                charSequence = Html.fromHtml(value)
                            }
                            DialogUtils.Builder(context).setMessage(charSequence)
                                .setNegativeButton("取消", R.color.color_999999, null)
                                .setPositiveButton("确定", R.color.color_2d5aff) {
                                    field?.removeAt(i)
                                    deferNotifyDataSetChanged()
                                }
                                .creat().show()
                        }
                    }
                }
            }
        }


    var itemTrainAssemble: MutableList<ItemCarInPreviewModel>? = null
        set(value) {
            field = value
            value?.let {
                if (it.isNotEmpty()) {
                    adapter = CommonAdapter(context, R.layout.item_carin_train, it) { _, t, i ->
                    }
                }
            }
        }
}
